//
//  UIView+QYView.m
//  DDCategory
//
//  Created by Dxc_iOS on 2018/7/19.
//  Copyright © 2018年 Dxc_123. All rights reserved.
//

#import "UIView+QYView.h"
#import <objc/runtime.h>
//定义
typedef void(^QY_ViewTappedBlock)(void);
@interface UIView ()
/** 单击手势事件回调的block */
@property (nonatomic, copy) QY_ViewTappedBlock qy_viewTappedBlock;
@end

@implementation UIView (QYView)
//------- 添加属性 -------//
static void *qy_viewTappedBlockKey = &qy_viewTappedBlockKey;




-(void)setQy_x:(CGFloat)qy_x
{
    CGRect frame = self.frame;
    frame.origin.x = qy_x;
    self.frame = frame;
}

- (CGFloat)qy_x
{
    return self.frame.origin.x;
}

- (void)setQy_y:(CGFloat)qy_y
{
    CGRect frame = self.frame;
    frame.origin.y = qy_y;
    self.frame = frame;
}

- (CGFloat)qy_y
{
    return self.frame.origin.y;
}

- (void)setQy_centerX:(CGFloat)qy_centerX
{
    CGPoint center = self.center;
    center.x = qy_centerX;
    self.center = center;
}

- (CGFloat)qy_centerX
{
    return self.center.x;
}

- (void)setQy_centerY:(CGFloat)qy_centerY
{
    CGPoint center = self.center;
    center.y = qy_centerY;
    self.center = center;
}

- (CGFloat)qy_centerY
{
    return self.center.y;
}

- (void)setQy_width:(CGFloat)qy_width
{
    CGRect frame = self.frame;
    frame.size.width = qy_width;
    self.frame = frame;
}

- (CGFloat)qy_width
{
    return self.frame.size.width;
}

- (void)setQy_height:(CGFloat)qy_height
{
    CGRect frame = self.frame;
    frame.size.height = qy_height;
    self.frame = frame;
}

- (CGFloat)qy_height
{
    return self.frame.size.height;
}

- (void)setQy_size:(CGSize)qy_size
{
    CGRect frame = self.frame;
    frame.size = qy_size;
    self.frame = frame;
}

- (CGSize)qy_size
{
    return self.frame.size;
}

- (void)setQy_origin:(CGPoint)qy_origin
{
    CGRect frame = self.frame;
    frame.origin = qy_origin;
    self.frame = frame;
}

- (CGPoint)qy_origin
{
    return self.frame.origin;
}

- (void)setQy_top:(CGFloat)qy_top
{
    CGRect frame = self.frame;
    frame.origin.y = qy_top;
    self.frame = frame;
}

- (CGFloat)qy_top
{
    return self.frame.origin.y;
}

- (void)setQy_bottom:(CGFloat)qy_bottom
{
    CGRect frame = self.frame;
    frame.origin.y = qy_bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)qy_bottom
{
    return self.qy_top + self.qy_height;
}

- (void)setQy_left:(CGFloat)qy_left
{
    CGRect frame = self.frame;
    frame.origin.x = qy_left;
    self.frame = frame;
}

- (CGFloat)qy_left
{
    return self.frame.origin.x;
}

- (void)setQy_right:(CGFloat)qy_right
{
    CGRect frame = self.frame;
    frame.origin.x = qy_right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)qy_right
{
    return self.frame.origin.x + self.frame.size.width;
}


#pragma mark - 初始化方法
+ (instancetype _Nonnull)qy_viewWithFrame:(CGRect)frame backgroundColor:(UIColor *_Nonnull)backgroundColor
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:backgroundColor];
    return view;
}

#pragma mark - UIView绑定的事件回调block

- (QY_ViewTappedBlock)qy_viewTappedBlock {
    return objc_getAssociatedObject(self, &qy_viewTappedBlockKey);
}

//

- (void)setQy_viewTappedBlock:(QY_ViewTappedBlock)qy_viewTappedBlock {
    objc_setAssociatedObject(self, &qy_viewTappedBlockKey, qy_viewTappedBlock, OBJC_ASSOCIATION_COPY);
}
- (void)qy_addViewTapped:(void(^)(void))tappedBlock {
    self.qy_viewTappedBlock = tappedBlock;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
    [self addGestureRecognizer:tapGesture];
}

// 单击view
- (void)viewTapped {
    !self.qy_viewTappedBlock ?: self.qy_viewTappedBlock();
}










#pragma mark - 找到view的父控制器
- (UIViewController *)qy_findViewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

#pragma mark - 给view切圆角
- (instancetype)qy_setCornerRadius:(CGFloat)cornerRadius {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    shapeLayer.path = path.CGPath;
    self.layer.mask = shapeLayer;
    self.layer.contentsScale = [[UIScreen mainScreen] scale];
    return self;
}

#pragma mark -给view某个角切圆角
- (instancetype)qy_cornerByRoundingCorners:(UIRectCorner)corners cornerRadius:(CGFloat)cornerRadius{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    self.layer.contentsScale = [[UIScreen mainScreen] scale];
    return self;
}

#pragma mark - 获取截图
- (UIImage *_Nonnull)qy_screenshot
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSData *imageData = UIImagePNGRepresentation(image);
    image = [UIImage imageWithData:imageData];
    
    return image;
}



@end
